﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Player;

namespace TestPlayerBase
{
    public partial class Form1 : Form
    {
        Dictionary<string, List<string>> devices;
        public Form1()
        {
            devices = PlayerBase.GetAreaDeviceList();

            InitializeComponent();
        }

        void player_PlayEnd(object sender, EventArgs e)
        {
            if (this.lBRecord.SelectedItem == null) return;
            int index = this.lBRecord.Items.IndexOf(this.lBRecord.SelectedItem);
            if (index >= this.lBRecord.Items.Count - 1) return;

            DateTime DT = (DateTime)this.lBRecord.Items[index + 1];
            this.lBRecord.SelectedItem = DT;

            if (player.Open("计算机学院", "楼顶大镜头1", DT)
                && player.Play())
            {
                tSSLStartTime.Text = "起始时间:" + this.player.StartTime.ToString();
                tSSLTotalTime.Text = string.Format("总时长:{0:D2}:{1:D2}:{2:D2}", this.player.TotalTime.Hours, this.player.TotalTime.Minutes, this.player.TotalTime.Seconds);
                tSSLTotalFrame.Text = "总帧数:" + this.player.TotalFrame;
                tPlay.Start();
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            foreach (var area in devices)
            {
                TreeNode TN = new TreeNode(area.Key);
                foreach (var device in area.Value) TN.Nodes.Add(device);
                this.tVDevice.Nodes.Add(TN);
            }
        }

        private void bPlay_Click(object sender, EventArgs e)
        {
            if (this.lBRecord.SelectedItem == null) return;
            DateTime DT = (DateTime)this.lBRecord.SelectedItem;
            if (player.Open("计算机学院", "楼顶大镜头1", DT)
                && player.Play())
            {
                tSSLStartTime.Text = "起始时间:" +this.player.StartTime.ToString();
                tSSLTotalTime.Text = string.Format("总时长:{0:D2}:{1:D2}:{2:D2}", this.player.TotalTime.Hours, this.player.TotalTime.Minutes, this.player.TotalTime.Seconds);
                tSSLTotalFrame.Text = "总帧数:" + this.player.TotalFrame;
                tPlay.Start();
            }

        }

        private void bStop_Click(object sender, EventArgs e)
        {
            tPlay.Stop();
            tSSLStartTime.Text = "起始时间:";
            tSSLTotalTime.Text = "总时长:";
            tSSLTotalFrame.Text = "总帧数:";
            tSSLSpeed.Text = "播放速度:";
            tSSLTime.Text = "当前时长:";
            tSSLFrame.Text = "当前帧数:";
            tSSLPosition.Text = "当前进度";
            player.Stop();
            player.Close();
        }

        private void bFast_Click(object sender, EventArgs e)
        {
            player.Fast();
        }

        private void bSlow_Click(object sender, EventArgs e)
        {
            player.Slow();
        }

        private void bPause_Click(object sender, EventArgs e)
        {
            tPlay.Stop();
            player.Pause();
        }

        private void bContinue_Click(object sender, EventArgs e)
        {
            tPlay.Start();
            player.Continue();
        }

        private void player_Paint(object sender, IPlayer.EventArgsPaint e)
        {
            Graphics G = e.Graphics;
            Rectangle rect = new Rectangle(10, 10, 10, 10);
            G.DrawRectangle(Pens.Yellow, rect);
        }

        private void tPlay_Tick(object sender, EventArgs e)
        {
            if (this.player != null)
            {
                tSSLTime.Text = string.Format("当前时长:{0:D2}:{1:D2}:{2:D2}:{3:D3}", this.player.PlayedTime.Hours, this.player.PlayedTime.Minutes, this.player.PlayedTime.Seconds, this.player.PlayedTime.Milliseconds);
                tSSLFrame.Text = "当前帧数:" + this.player.FrameNumber;
                tSSLPosition.Text = string.Format("当前进度:{0:0.0000%}",this.player.PlayPosition);
                tSPBPosition.Value = Convert.ToInt32(this.player.PlayPosition * 100);
                if (this.player.Speed > 0)
                    tSSLSpeed.Text = "播放速度:快放×" + this.player.Speed;
                else if (this.player.Speed < 0)
                    tSSLSpeed.Text = "播放速度:慢放÷" + (-this.player.Speed);
                else
                    tSSLSpeed.Text = "播放速度:正常";
                
            }
        }

        private void hSBSetPosition_Scroll(object sender, ScrollEventArgs e)
        {
            this.player.PlayPosition = (float)hSBSetPosition.Value / 100;
        }
        string area = string.Empty;
        string device = string.Empty;
        DateTime datetime = DateTime.Now;
        private void tVDevice_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tVDevice.SelectedNode != null
                && tVDevice.SelectedNode.Parent != null)
            {
                area = tVDevice.SelectedNode.Parent.Text;
                device = tVDevice.SelectedNode.Text;
                mCRecord.SetDate(datetime);
                //mCRecord.BoldedDates = PlayerBase.GetDays(area, device, datetime);
            }
        }

        private void mCRecord_DateChanged(object sender, DateRangeEventArgs e)
        {
            if (area != string.Empty && device != string.Empty)
            {
                if (datetime.Year != e.Start.Year
                    || datetime.Month != e.Start.Month)
                {
                    datetime = e.Start;
                    //mCRecord.BoldedDates = PlayerBase.GetDays(area, device, datetime);
                }

                if (datetime.Year != e.Start.Year
                    || datetime.Month != e.Start.Month
                    || datetime.Day != e.Start.Day)
                {
                    datetime = e.Start;
                    DateTime[] datetimes = PlayerBase.GetAreaDeviceDateRecords(area, device, datetime);
                    lBRecord.Items.Clear();
                    foreach (var item in datetimes)
                    {
                        lBRecord.Items.Add(item);
                    }
                }
            }
        }



    }
}
